<?php
/**
 * CalDAV FSExt backend
 *
 * This backend understands Calendars.
 *
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_CalDAV_FSExt_Directory extends Slash_ACL_FSExt_Directory
{
	
	public function getChild($name)
	{
		$child = null;
		$path = $this->path . '/' . $name;
		if (!file_exists($path))
		{
			throw new Sabre_DAV_Exception_FileNotFound("File '$path' could not be located");
		}
		if ($name=='.' || $name=='..')
		{
			throw new Sabre_DAV_Exception_Forbidden('Permission denied to . and ..');
		}

		if (is_dir($path))
		{
			$calendarRoot = Slash_CalDAV_Server::getCalendarRoot();
			$rootPath = Slash_CalDAV_Server::getRootDirectory();
			//rRootPath = $calendarRoot->uri;
			//If we're in the process of creating the calendarRoot, this will fail.
			Slash_Debug::debug("CalDAV_FSExt_Directory getChild: path=$path, calendarRoot=$calendarRoot");
			//TODO: nasty hack
			if ($path == "$rootPath/$calendarRoot")
			{
				//Slash_Debug::debug("CalDAV_FSExt_Directory getChild: returning the calendarRoot");
				$child = new Slash_CalDAV_FSExt_CalendarRoot($path);
			} else if ($this instanceof Slash_CalDAV_FSExt_CalendarRoot)
			{
				//Slash_Debug::debug("CalDAV_FSExt_Directory getChild: returning a calendar");
				$child = new Slash_CalDAV_FSExt_Calendar($path);
			} else {
				//Slash_Debug::debug("CalDAV_FSExt_Directory getChild: delegating to parent");
				$child = parent::getChild($name);
			}
		} else if ($this instanceof Slash_CalDAV_FSExt_Calendar)
		{
			$child = new Slash_CalDAV_FSExt_CalendarComponent($path);
		} else {
			$child = parent::getChild($name);
		}
		//Slash_Debug::debug("CalDAV_FSExt_Directory getChild: returning " . $child->path);
		return $child;
	}


	/**
	 * Delete a calendar and all it's objects
	 *
	 * @param string $calendarId
	 * @return void
	 */
	public function deleteCalendar($name)
	{
		$cal = $this->getChild($name);
		return $cal->delete();
	}

}
